/**
 * Copyright by www.xfok.net
 */
package net.xfok.ui.animation;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * @author 2010-12-3 Howard
 *
 */
public class RotateCube {
	private View left,right;
	private RotateCubeAnimation leftAnimation, rightAnimation;
	private float mCenterX, mCenterY;
	
	public RotateCube(Context context){
		this.mCenterX=getWidth(context)/2;
		this.mCenterY=getWidth(context)/2;	
	}
	
	public RotateCube(Context context,View left,View right){
		this.left=left;
		this.right=right;
		this.mCenterX=getWidth(context)/2;
		this.mCenterY=getWidth(context)/2;	
	}
	
	public void rotateRight(){
		rotate(left, right, true);
	}
	
	public void rotateLeft(){
		rotate(left, right, false);
	}
	
	public void rotate(View left,View right,boolean direction){
		int angel=direction?1:-1;
		leftAnimation = new RotateCubeAnimation(0, RotateCubeAnimation.RIGHT_ANGEL*angel, 0, 0, mCenterX, mCenterY);
		rightAnimation = new RotateCubeAnimation((-1)*RotateCubeAnimation.RIGHT_ANGEL*angel, 0, 0, 0, mCenterX, mCenterY);
		leftAnimation.setFillAfter(true);
		leftAnimation.setDuration(RotateCubeAnimation.ROTATE_DURATION);
		rightAnimation.setFillAfter(true);
		rightAnimation.setDuration(RotateCubeAnimation.ROTATE_DURATION);
		left.startAnimation(leftAnimation);
		right.startAnimation(rightAnimation);
	}
	
	public void rotate(boolean direction){
		int angel=direction?1:-1;
		leftAnimation = new RotateCubeAnimation(0, RotateCubeAnimation.RIGHT_ANGEL*angel, 0, 0, mCenterX, mCenterY);
		rightAnimation = new RotateCubeAnimation((-1)*RotateCubeAnimation.RIGHT_ANGEL*angel, 0, 0, 0, mCenterX, mCenterY);
		leftAnimation.setFillAfter(true);
		leftAnimation.setDuration(RotateCubeAnimation.ROTATE_DURATION);
		rightAnimation.setFillAfter(true);
		rightAnimation.setDuration(RotateCubeAnimation.ROTATE_DURATION);
		left.startAnimation(leftAnimation);
		right.startAnimation(rightAnimation);
	}
	
	public static int getWidth(Context cx) {
		DisplayMetrics dm = cx.getApplicationContext().getResources()
				.getDisplayMetrics();
		return dm.widthPixels;
	}

	public static int getHeight(Context cx) {
		DisplayMetrics dm = cx.getApplicationContext().getResources()
				.getDisplayMetrics();
		return dm.heightPixels;
	}

	public View getLeft() {
		return left;
	}

	public void setLeft(View left) {
		this.left = left;
	}

	public View getRight() {
		return right;
	}

	public void setRight(View right) {
		this.right = right;
	}
	
}
